import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMailUtil {

    /**
     * 发送注册验证码邮件
     * @param recevices		接收人的邮箱
     * @return code         返回发出去的验证码
     */
    public String sendRegisterEmail(String recevices){
        String code = code();       //验证码
        String name = "云腾图书馆用户";//收件人的姓名
        String from_email = "3162713296@qq.com";    //发件人的邮箱
        String pwd = "ojwlhyxjloabdgdj";            //发件人的授权码
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");		//使用smpt的邮件传输协议
        props.setProperty("mail.smtp.host", "smtp.qq.com");		    //主机地址
        props.setProperty("mail.smtp.auth", "true");		        //授权通过

        Session session = Session.getInstance(props);	        	//通过我们的这些配置，得到一个会话程序

        try {

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from_email));		//设置发件人
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recevices,"用户","utf-8"));		//设置收件人
            message.setSubject("云腾图书馆","utf-8");		//设置主题
            message.setSentDate(new Date());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String str = "<!DOCTYPE html><html><head><meta charset='UTF-8'></head><body><p style='font-size: 20px;font-weight:bold;'>尊敬的："+name+"，您好！</p>"
                    + "<p style='text-indent:2em; font-size: 20px;'>欢迎注册云腾图书馆账号，您本次的注册码是 "
                    + "<span style='font-size:30px;font-weight:bold;color:red'>" + code + "</span>，3分钟之内有效，请勿泄露或转发。如非本人操作，请忽略本邮件。</p>"
                    + "<p style='text-align:right; padding-right: 20px;'"
                    + "<a href='http://www.hyycinfo.com' style='font-size: 18px'>云顶书院-云腾项目组</a></p>"
                    + "<span style='font-size: 18px; float:right; margin-right: 60px;'>" + sdf.format(new Date()) + "</span></body></html>";

            Multipart mul=new MimeMultipart();   //新建一个MimeMultipart对象来存放多个BodyPart对象
            BodyPart mdp=new MimeBodyPart();     //新建一个存放信件内容的BodyPart对象
            mdp.setContent(str, "text/html;charset=utf-8");
            mul.addBodyPart(mdp);    //将含有信件内容的BodyPart加入到MimeMultipart对象中
            message.setContent(mul); //把mul作为消息内容

            message.saveChanges();

            //创建一个传输对象
            Transport transport=session.getTransport("smtp");

            //建立与服务器的链接  465端口是 SSL传输
            transport.connect("smtp.qq.com", 587, from_email, pwd);

            //发送邮件
            transport.sendMessage(message,message.getAllRecipients());

            //关闭邮件传输
            transport.close();

        } catch (UnsupportedEncodingException | MessagingException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 发送密保验证码邮件
     * @param recevices		接收人的邮箱
     * @return code         返回发出去的验证码
     */
    public String sendPasswordEmail(String recevices){
        String code = code();            //验证码
        String name = "云腾图书馆用户";    //收件人的姓名
        String from_email = "3162713296@qq.com";    //发件人的邮箱
        String pwd = "ojwlhyxjloabdgdj";            //发件人的授权码
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");		//使用smpt的邮件传输协议
        props.setProperty("mail.smtp.host", "smtp.qq.com");		    //主机地址
        props.setProperty("mail.smtp.auth", "true");		        //授权通过

        Session session = Session.getInstance(props);	        	//通过我们的这些配置，得到一个会话程序

        try {

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from_email));		//设置发件人
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recevices,"用户","utf-8"));		//设置收件人
            message.setSubject("云腾图书馆","utf-8");		//设置主题
            message.setSentDate(new Date());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String str = "<!DOCTYPE html><html><head><meta charset='UTF-8'></head><body><p style='font-size: 20px;font-weight:bold;'>尊敬的："+name+"，您好！</p>"
                    + "<p style='text-indent:2em; font-size: 20px;'>您本次修改密码的验证码是 "
                    + "<span style='font-size:30px;font-weight:bold;color:red'>" + code
                    + "</span>，3分钟之内有效，请勿泄露或转发。如非本人操作，请忽略本邮件。<br><br> "
                    + "温馨提示：<br>"
                    + "尽量设置没有规律但自己容易记住的数字字母组合作为密码，密码长度可以根据自己方便记忆设置;<br>"
                    + "密码作为私密的信息请勿泄露给其他人，以免帐号被盗造成不必要的损失</p>"
                    + "<p style='text-align:right; padding-right: 20px;'"
                    + "<a href='http://www.hyycinfo.com' style='font-size: 18px'>云顶书院-云腾项目组</a></p>"
                    + "<span style='font-size: 18px; float:right; margin-right: 60px;'>" + sdf.format(new Date()) + "</span></body></html>";

            Multipart mul=new MimeMultipart();   //新建一个MimeMultipart对象来存放多个BodyPart对象
            BodyPart mdp=new MimeBodyPart();     //新建一个存放信件内容的BodyPart对象
            mdp.setContent(str, "text/html;charset=utf-8");
            mul.addBodyPart(mdp);    //将含有信件内容的BodyPart加入到MimeMultipart对象中
            message.setContent(mul); //把mul作为消息内容

            message.saveChanges();

            //创建一个传输对象
            Transport transport=session.getTransport("smtp");

            //建立与服务器的链接  465端口是 SSL传输
            transport.connect("smtp.qq.com", 587, from_email, pwd);

            //发送邮件
            transport.sendMessage(message,message.getAllRecipients());

            //关闭邮件传输
            transport.close();

        } catch (UnsupportedEncodingException | MessagingException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     *
     * @return   返回产生的随机六位验证码
     */
    public String code(){
        String[] letters = new String[] {
                "q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
                "A","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M",
                "0","1","2","3","4","5","6","7","8","9"};
        String stringBuilder ="";
        for (int i = 0; i < 6; i++) {
            stringBuilder = stringBuilder + letters[(int)Math.floor(Math.random()*letters.length)];
        }
        return stringBuilder;
    }


}

